data_panel = dta_overtime |> 
  mutate(
    accuracy_tri = case_when(
      accuracy %in% 1:2 ~ 1,
      accuracy == 3 ~ 0,
      accuracy %in% 4:5 ~ -1,
      .default = NA_integer_
    ),
    accuracy_dgr = case_when(
      accuracy %in% 1:3 ~ 0, # Flipped
      accuracy %in% 4:5 ~ 1,
      .default = NA_integer_
    ),
    accuracy_agr = case_when(
      accuracy %in% 1:2 ~ 1,
      accuracy %in% 3:5 ~ 0,
      .default = NA_integer_
    ),
    accuracy_rev = 6 - accuracy,
    is_denier = as.factor(accuracy_dgr),
    period = case_when(
      date >= as.Date("2022-11-08") & date <= as.Date("2022-11-29") ~ "Post-Election",
      date >= as.Date("2023-01-12") & date <= as.Date("2023-02-03") ~ "Height",
      date >= as.Date("2024-01-24") & date <= as.Date("2024-02-15") ~ "Current",
      .default = "Other"
    ),
    maga = case_when(
      pid == "Democrat" ~ 0,
      maga == 1 ~ 1,
      .default = 0
    ),
    pid = as.factor(pid),
    age = 2022 - birthyr,
    strong = pid7 %in% c(1,7),
    male = ifelse(gender == 1, 1, 0),
    white = ifelse(race == 1, 1, 0),
    college = ifelse(educ %in% 5:6, 1, 0),
    married = ifelse(marstat == 1, 1, 0),
    children = ifelse(child18 == 1, 1, 0),
    employed = case_when(
      employ == 1 ~ 2,
      employ == 2 ~ 1,
      .default = 0 
    ),
    bornagain = ifelse(pew_bornagain == 1, 1, 0),
    voted_2020 = ifelse(presvote20post == 6, 0, 1),
    registered = ifelse(votereg == 1, 1, 0),
    inc_terc = case_when(
      faminc_new %in% 1:5 ~ "Lower",
      faminc_new %in% 6:9 ~ "Middle",
      faminc_new %in% 10:16 ~ "Upper",
      .default = "Middle" # Median imputation
    ),
    # Attitudes
    trust_binary = ifelse(general_trust == 1, 1, 0),
    vote_import_binary = ifelse(vote_importance %in% 1:2, 1, 0),
    corruption_binary = ifelse(institutional_corruption %in% 4:5, 1, 0),
    response_binary = ifelse(institutional_response %in% 4:5, 1, 0),
    pride_binary = ifelse(pride %in% 1:2, 1, 0),
    fair_binary = ifelse(fair_treatment %in% 1:2, 1, 0),
    democracy_binary = ifelse(democracy_importance %in% 1:2, 1, 0)
  ) |> 
  select(
    # DV
    is_denier, period, accuracy,
    # Demos
    pid, maga, strong, age, male, white, college, married,
    children, employed, bornagain, voted_2020, registered, inc_terc,
    # Attitudes
    uid,
    democrat_therm_1, republican_therm_1, ends_with("_binary"), weight
  ) |> 
  mutate(across(all_of(c("age","democrat_therm_1","republican_therm_1")), ~scale(.x))) |> 
  drop_na()

vars = names(data_panel)[-which(names(data_panel) %in% c("is_denier","accuracy_rev", "weight", "period","uid","inc_terc","pid"))]

panel_res = map_dfr(vars, \(x){
  print(x)
  xs = as.symbol(x)
  d = data_panel |> 
    group_by(uid) |> 
    filter(n() > 1) |> 
    ungroup() |> 
    mutate(is_denier = as.numeric(is_denier) - 1)
  f = as.formula(paste0("is_denier ~ ", x, "| uid"))
  mod = fixest::feols(f, data = d,
              weights = d$weight)
  broom::tidy(mod) |> 
    mutate(
      iv = x,
      dv = "Binary DV")
})

panel_res_c = map_dfr(vars, \(x){
  print(x)
  xs = as.symbol(x)
  d = data_panel |> 
    group_by(uid) |> 
    filter(n() > 1) |> 
    ungroup() |> 
    mutate(is_denier = as.numeric(is_denier) - 1)
  f = as.formula(paste0("accuracy ~ ", x, "| uid"))
  mod = fixest::feols(f, data = d,
              weights = d$weight)
  broom::tidy(mod) |> 
    mutate(iv = x,
           dv = "Continuous DV")
})

fig_s5 = panel_res |> 
  bind_rows(panel_res_c) |> 
  mutate(
    term_clean = case_match(
      iv,
      "employed" ~ "Employed",
      "male" ~ "Male",
      "children" ~ "Parent",
      "college" ~ "College-Educated",
      "registered" ~ "Registered Voter",
      "bornagain" ~ "Evangelical",
      "voted_2020" ~ "2020 Voter",
      "married" ~ "Married",
      "white" ~ "White",
      "pidRepublican" ~ "PID: Republican",
      "pidIndependent" ~ "PID: Independent",
      "inc_terc_Middle" ~ "Income (Middle Terc.)",
      "inc_terc_Upper" ~ "Income (Upper Terc.)",
      "strong" ~ "Strong Partisan",
      "age" ~ "Age",
      "trust_binary" ~ "General Trust",
      "vote_import_binary" ~ "Vote Efficacy",
      "democrat_therm_1" ~ "Democrat FT",
      "republican_therm_1" ~ "Republican FT",
      "maga" ~ "MAGA (self-identified)",
      "corruption_binary" ~ "Perceived Corruption",
      "fair_binary" ~ "Fair Political Treatment",
      "pride_binary" ~ "Proud to be American",
      "democracy_binary" ~ "Democracy Importance",
      "response_binary" ~ "Perceived Responsiveness"
    ),
    term_cat = case_when(
      iv %in% c("pid","employed","male","children",
                "college","registered","bornagain","voted_2020","married","white",
                "inc_terc_Middle", "inc_terc_Upper","age","maga") ~ "Demographic",
      .default = "Attitudinal"
    )) |>
  filter(term_cat == "Attitudinal") |> 
  ggplot(aes(x = estimate, y = reorder(term_clean,estimate))) +
  geom_pointrange(aes(xmin = estimate - 1.96*std.error,
                      xmax = estimate + 1.96*std.error),
                  position = position_dodge(.5)) +
  facet_wrap(~ dv, ncol = 2, scales = "free") +
  labs(x = "OLS Coefficient", y = NULL) +
  geom_vline(xintercept = 0, lty = 2, color = "grey25") +
  theme_prl()

ggsave(here("Figures","figure_s5.pdf"), fig_s5,
       dpi = 600, units = "in", width = 8, height = 4)
